package xcmg.device.web.delivery;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.delivery.ReceiptInService;
import xcmg.device.service.vo.delivery.ManualFinishVO;
import xcmg.device.service.vo.delivery.ReceiptInAddVO;
import xcmg.device.service.vo.delivery.ReceiptInVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

@RestController
@RequestMapping("/receiptIn")
public class ReceiptInController extends BasicController {

    @Autowired
    private ReceiptInService receiptInService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<ReceiptInVO> condition)throws Exception{
        ReceiptInVO buyBackVO = condition.getFilters();
        buyBackVO.setOrgId(getOrgId());
        condition.setFilters(buyBackVO);
        PageCommonVO pageCommonVO = receiptInService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody ReceiptInAddVO receiptInAddVO)throws Exception{
        ReceiptInVO buyBackVO = receiptInAddVO.getHeaderInfo();
        buyBackVO.setCreateId(getUserId());
        buyBackVO.setOrgId(getOrgId());
        buyBackVO.setCreateName(getUserName());
        receiptInAddVO.setHeaderInfo(buyBackVO);
        try {
            return actionResult(receiptInService.add(receiptInAddVO, getUserId(), getOrgId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id) throws Exception {
        return actionValueResult(receiptInService.item(id,getOrgId()));
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody ReceiptInAddVO receiptInAddVO)throws Exception{
        try {
            return actionResult(receiptInService.update(receiptInAddVO));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/remove/{id}",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@PathVariable("id") String id)throws Exception{
        try {
            return actionResult(receiptInService.remove(id));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void export( String[] ids)throws Exception {
        receiptInService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void exportAll(ReceiptInVO condition)throws Exception {
        condition.setOrgId(getOrgId());
        receiptInService.exportAll(condition);
    }

    @RequestMapping(value = "/manualFinish", method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation("运单入库结单")
    public ActionResult manualFinish(@RequestBody ManualFinishVO manualFinishVO) throws Exception {
        try {
            return actionResult(receiptInService.manualFinish(manualFinishVO.getBillNo(), manualFinishVO.getMaterialsNo(), getOrgId()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
    }
}
